﻿using System;
using System.Text;

namespace CodeContrib
{
	public class RandomParagraph : IRandom<string>
	{
		private readonly RandomSentence _randomSentence;
		private readonly RandomInteger _randomSentenceCount;

		public static int MaxSentenceCount { get; set; }
		public static int MinSentenceCount { get; set; }

		static RandomParagraph()
		{
			MaxSentenceCount = 10;
			MinSentenceCount = 1;
		}

		public RandomParagraph() : this(MinSentenceCount, MaxSentenceCount)
		{
		}

		public RandomParagraph(int maxSentenceCount) : this(MinSentenceCount, maxSentenceCount)
		{
		}

		public RandomParagraph(int minSentenceCount, int maxSentenceCount)
		{
			Guard.NotNegative(minSentenceCount, "minSentenceCount");
			Guard.NotNegativeOrZero(maxSentenceCount, "maxSentenceCount");

			if (minSentenceCount > maxSentenceCount)
			{
				throw new ArgumentOutOfRangeException("minSentenceCount", minSentenceCount, "'minSentenceCount' cannot be greater than 'maxSentenceCount'");
			}

			_randomSentenceCount = new RandomInteger(minSentenceCount, maxSentenceCount);
			_randomSentence = new RandomSentence(maxSentenceCount);
		}

		public string Next()
		{
			var paragraph = new StringBuilder();
			paragraph.Append(_randomSentence.Next().CharsToUpper(0));

			var wordCount = _randomSentenceCount.Next();

			for (var i = 0; i < wordCount - 1; i++)
			{
				paragraph.Append(" ");
				paragraph.Append(_randomSentence.Next());
			}

			return paragraph.ToString();
		}
	}
}